﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Collections;
using System.Data;
using CE.Database;

/// <summary>
/// Summary description for CarBaseValueDB
/// </summary>
/// 
namespace CE.Provider
{
    public class CarBaseValueDB
    {
        public CarBaseValueDB()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public Int64 Insert(CarBaseValue carbasevalue)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarBaseValueInsertUpdateSingleItem]";

            SortedList _sPara = new SortedList();
            _sPara.Add("@BaseId", carbasevalue.BaseId);
            _sPara.Add("@ProviderCompanyId", carbasevalue.ProviderCompanyId);
            _sPara.Add("@CXCoverTypeId", carbasevalue.CXCoverTypeId);
            _sPara.Add("@PackageId", carbasevalue.PackageId);
            _sPara.Add("@BaseValue", carbasevalue.BaseValue);
            _sPara.Add("@RefCEUserId", carbasevalue.RefCEUserId);
            _sPara.Add("@Status", carbasevalue.Status == true ? 1 : 0);
           
            _result = _db.ExecuteSP(_spName, _sPara);

            return _result;
        }

        public bool Delete(Int64 CXCoverTypeId, Int64 ProviderCompanyId, Int64 PackageId)
        {
            MyDatabase _db = new MyDatabase();
            Int64 _result = -1;
            string _spName = "[sprocProviderCarBaseValueDeleteSingleItem]";

            SortedList _sPara = new SortedList();

            #region Parameter
            _sPara.Add("@CXCoverTypeId", CXCoverTypeId);
            _sPara.Add("@ProviderCompanyId", ProviderCompanyId);
            _sPara.Add("@PackageId", PackageId);
            #endregion

            _result = _db.ExecuteSP(_spName, _sPara);

            return _result > 0;
        }

        public CarBaseValue GetCarBaseValue(Int64 ProviderCompanyId, Int32 CoverTypeId ,Int64 PackageId)
        {
            CarBaseValue _carbasevalue = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarBaseValueSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);
            _spara.Add("@CXCoverTypeId", CoverTypeId);
            _spara.Add("@PackageId", PackageId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carbasevalue = new CarBaseValue();
                    _carbasevalue = FillDataRecord(_dt, 0);
                }
            }
            return _carbasevalue;
        }

        public CarBaseValue GetCarBaseValue(Int64 ProviderCompanyId)
        {
            CarBaseValue _carbasevalue = null;
            MyDatabase _db = new MyDatabase();
            string _spName = "sprocProviderCarBaseValueSelectSingleItem";

            SortedList _spara = new SortedList();
            _spara.Add("@ProviderCompanyId", ProviderCompanyId);

            using (DataTable _dt = _db.GetDataTableBySP(_spName, _spara))
            {
                if (_dt != null && _dt.Rows.Count > 0)
                {
                    _carbasevalue = new CarBaseValue();
                    _carbasevalue = FillDataRecord(_dt, 0);
                }
            }
            return _carbasevalue;
        }

        private CarBaseValue FillDataRecord(DataTable myDataTable, int RowId)
        {
            CarBaseValue myCarBaseValue = new CarBaseValue();
            myCarBaseValue.BaseId = Convert.ToInt64(myDataTable.Rows[RowId]["BaseId"].ToString());
            myCarBaseValue.ProviderCompanyId = Convert.ToInt32(myDataTable.Rows[RowId]["ProviderCompanyId"].ToString());
            myCarBaseValue.CXCoverTypeId = Convert.ToInt32(myDataTable.Rows[RowId]["CXCoverTypeId"].ToString());
            myCarBaseValue.PackageId = Convert.ToInt32(myDataTable.Rows[RowId]["PackageId"].ToString());
            myCarBaseValue.BaseValue = Convert.ToInt32(myDataTable.Rows[RowId]["BaseValue"].ToString());
            myCarBaseValue.RefCEUserId = Convert.ToInt64(myDataTable.Rows[RowId]["RefCEUserId"].ToString());
            myCarBaseValue.Status = Convert.ToBoolean(myDataTable.Rows[RowId]["Status"].ToString());
            return myCarBaseValue;
        }
    }
}